define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/tied/verifyCode.html",
    "wTools",
    "area",
    "validate"

], function ($, _, Backbone, template) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        $timer:null,
        template: _.template(template),
        events: {
            "tap #btn": "btn",
            "tap .getCode":"getCode",
            "input #smsCode":"limitLen"
        },
        initialize: function () {
            $("#head_back").hide();

            var postData = {
                "token": wData.urlData.token
            }, that = this;

            wAjaxForm("getTokenInfo", "", postData, function (result) {
                if (result.success) {
                    wData.returnUrl = result.tokenInfo.returnUrl;
                    wData.userNo = result.tokenInfo.userNo;
                    wData.trxId = result.tokenInfo.trxId;
                    that.render(wData);
                    that.setTimer();
                }else{
                    wTips("请重试");
                }
            });
        },
        back: function () {
            history.back();
        },
        render: function (data) {
            var html = this.template(data);
            this.$el.html(html);
        },
        btn: function () {
            if($('#btn').hasClass('disabled')){
                return;
            }
            var postData = {
                'token':wData.urlData.token,
                'smsCode':$('#smsCode').val()
            };
            wAjaxForm('authconfirm', '', postData, function(result){
                result = JSON.parse(result);
                if( result.bizStsCd == '1' && result.status == "S"){
                    wData.tip = {
                        img: '<img src="images/icon_success2.png">',
                        text: '您好，您已成功绑定尾号为'+wData.bankCode.substring(wData.bankCode.length-4)+'的银行卡'
                    }
                    wData.status = 'S';
                    location.hash = 'bindResult';
                }
                else if(result.bizStsCd == '0' && result.sysRtnCd == 'F1001') {
                    wTips('短信验证码错误');
                }
                else if(result.bizStsCd == '0' && result.sysRtnCd == 'F0004') {
                    wTips('短信验证码失效');
                }
                else{
                    wData.tip = {
                        img: '<img src="images/icon_fail2.png">',
                        text: '您好，您本次绑卡异常，请重新绑卡'
                    }
                    location.hash = 'bindResult';
                }
            })
        },
        getCode: function() {
            if($('.getCode').hasClass('disabled')){
                return;
            }
            var postData = {
                'token':wData.urlData.token
            };
            var that = this;
            wAjaxForm('msgRetrySend', '', postData, function(result) {
                result = JSON.parse(result);
                if( result.bizStsCd == '1'){
                    that.setTimer();
                    $('.getCode').addClass('disabled');
                }
                else{
                    wTips('请重试');
                }
            });

        },
        setTimer: function() {
            var sec = 60;
            var timer = setInterval(function() {
                sec--;
                if(sec==0){
                    clearInterval(timer);
                    var str = '获取';
                    $('.getCode').removeClass('disabled');
                }else{
                    var str = sec+'s';
                }
                $('.getCode').html(str);
            },1000);
        },
        isEmpty: function() {
            var _el = $('.codeBox').find('input');
            for(var i = 0; i<_el.size();i++){
                if(_el.eq(i).val()==''){
                    this.$isEmpty = true;
                    break;
                }else{
                    this.$isEmpty = false;
                }
            }
            if(!this.$isEmpty){
                $('#btn').removeClass('disabled');
            }
            else{
                $('#btn').addClass('disabled');
            }
            return this.$isEmpty;
        },
        limitLen: function(event) {
            var lengths = 0;
            var id = event.target.id
            if(id == 'smsCode'){
                lengths = 6;
            }

            limitLength($('#'+id),lengths);
            nonNumeric($('#'+id));
            this.isEmpty();
        },
        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            delete this;
        }
    });
});